fix: compile error when deserializing into types using ordered_map#5128
Open
ssam18 wants to merge 4 commits intonlohmann:developfrom
Open
fix: compile error when deserializing into types using ordered_map#5128ssam18 wants to merge 4 commits intonlohmann:developfrom
ssam18 wants to merge 4 commits intonlohmann:developfrom
Conversation
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @ssam18 |
When passing a json value using brace initialization with a single element
(e.g., `json j{someObj}` or `foo({someJson})`), C++ always prefers the
initializer_list constructor over the copy/move constructor. This caused
the value to be unexpectedly wrapped in a single-element array.
This bug was previously compiler-dependent (GCC wrapped, Clang did not),
but Clang 20 started matching GCC behavior, making it a universal issue.
Fix: In the initializer_list constructor, when type deduction is enabled
and the list has exactly one element, copy/move it directly instead of
creating a single-element array.
Before:
json obj = {{"key", 1}};
json j{obj}; // -> [{"key":1}] (wrong: array)
foo({obj}); // -> [{"key":1}] (wrong: array)
After:
json j{obj}; // -> {"key":1} (correct: copy)
foo({obj}); // -> {"key":1} (correct: copy)
To explicitly create a single-element array, use json::array({value}).
Fixes the issue nlohmann#5074
Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
std::transform + std::inserter on vector-backed ordered_map triggers element shifting code in GCC's <algorithm>, which requires pair::operator=. That is deleted when the key is const (e.g. ordered_map<string, string>). Replace with a range-for + emplace so no assignment is ever needed. Fix for the bug nlohmann#5122 Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
6401b3b to
f906482
Compare
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @ssam18 |
Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @ssam18 |
1 similar comment
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @ssam18 |
nlohmann
requested changes
Apr 1, 2026
| yield different results (`#!json [true]` vs. `#!json true`)? | ||
|
|
||
| This is a known issue, and -- even worse -- the behavior differs between GCC and Clang. The "culprit" for this is the library's constructor overloads for initializer lists to allow syntax like | ||
| Starting from this version, single-value brace initialization is treated as copy/move instead of wrapping in a single-element array: |
Owner
There was a problem hiding this comment.
This is a breaking change we cannot accept. Please revert.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When deserializing into a struct that contains an
ordered_mapfield (e.g. viaNLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT), GCC fails to compile becausestd::transform+std::insertertriggers the element-shifting path in<algorithm>, which requirespair::operator=deleted when the key isconst.Replaced the transform with a simple range-for loop and
emplace, which constructs in-place and never tries to assign.